package cos.utils;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class Preferences {
	private static Preferences instance;

	private final Map<String, String> transDirections;

	private String translateDirection = "en-ru";

	private boolean isProxySetted;

	private String proxyHost = "";

	private String proxyPort = "";

	private Preferences() {
		transDirections = new HashMap<String, String>();
		transDirections.put("En - Ru", "en-ru");
		transDirections.put("It - Ru", "it-ru");
		transDirections.put("Fr - Ru", "fr-ru");

		BufferedReader bufRead;
		try {
			bufRead = new BufferedReader(new FileReader("conf/proxy.dat"));

			StringBuilder sb = new StringBuilder();

			// Read first line
			String line = bufRead.readLine();
			while (line != null) {
				sb.append(line);
				line = bufRead.readLine();
			}
			bufRead.close();
			line = sb.toString();
			if (!StringUtils.isNullOrEmpty(line)) {
				String[] split = line.split(":");
				setProxy(split[0], split[1]);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static synchronized Preferences getInst() {
		if (instance == null)
			instance = new Preferences();
		return instance;
	}

	public String getTranslationDirection() {
		return translateDirection;
	}

	public Collection<String> getTranslDirections() {
		return transDirections.keySet();
	}

	public void changeTranslateDirection(String in) {
		translateDirection = transDirections.get(in);
	}

	public String getProxyHost() {
		return proxyHost;
	}

	public String getProxyPort() {
		return proxyPort;
	}

	public void setProxy(String host, String port) {
		proxyHost = host;
		proxyPort = port;
		isProxySetted = true;
		if (!StringUtils.areNullOrEmpty(proxyHost, proxyPort)) {
			System.setProperty("http.proxyHost", proxyHost);
			System.setProperty("http.proxyPort", proxyPort);
		}

		// save
	}
}
